import {
	objDeepCopy,
	getDateString
} from "@/assets/js/common.js";
import commonObject from "@/assets/js/xm.js";
let Controller = {
	created() {
		this.init();
	},
	mounted() {

	},
	methods: {
		init() {
			this.getMainList(this);
			this.getCardMsg(this,2);
			
			this.getStatusSelect(this,'orderStatus',1)// 订单状态
			this.getStatusSelect(this,'payStatus',9)// 支付状态
			this.getStatusSelect(this,'orderAfterSaleStatus',10)// 售中售后状态
			
			this.getSupplierShop(this, undefined, 4) //发货门店
		},
		//获取卡片信息
		getCardMsg(that,type){
			var apiTest = this.apiTest;
			this.$ajax({
				method: 'get',
				url: apiTest + '/sale/saleOrder/statistics/' + type,
			}).then((res) => {
				var data = res.data;
				that.Model.configArr = [];
				if (data.status == 200) {
					var result = data.result;
					var obj;
					
					for (let i = 0; i < result.length; i++) {
						obj = {
							value:result[i].tabIndex,
							color:result[i].color,
							label:result[i].tabName,
							count:result[i].total,
							attributeValue:result[i].attributeValue,
							attributeName:result[i].attributeName,
						}
						that.Model.configArr.push(obj);
					}
					
				} else {
					that.tips(that, 'warning', data.msg);
				}
			
			});
		},
		//获取 订单状态and支付状态 售中售后状态  下拉框
		getStatusSelect(that,type,index){
			var apiTest = this.apiTest;
			this.$ajax({
				method: 'post',
				url: apiTest + '/base/systemDictionary/getByTypeToMap/' + type,
				data:{}
			}).then((res) => {
				var data = res.data;
				// that.Model.configArr = [];
				if (data.status == 200) {
					var result = data.result;
					var obj;
					var arr=[]
					Object.keys(result).forEach(function(key){
						// console.log(key,result[key]);
						obj = {
							label: result[key].value,
							value: result[key].key
						}
						arr.push(obj)
					});
					
					that.Model.filterOption.items[index].data = objDeepCopy(arr);
					
				} else {
					that.tips(that, 'warning', data.msg);
				}
			
			});
		},
		//获取 门店下拉框
		getSupplierShop(that, city, index) {
			var apiTest = this.apiTest;
			this.$ajax({
				method: 'post',
				url: apiTest + '/supplier/supplierShop/getList',
				data: {
					city: city
				}
			}).then((res) => {
				var data = res.data;
				if (data.status == 200) {
					that.Model.deliverStores = [];  //发货门店
					var result = data.result;
					if (result) {
						var obj;
						var arr = [];
						for (let i = 0; i < result.length; i++) {
							obj = {
								label:result[i].theNameOfTheStore,
								value:result[i].theFactory
							}
							arr.push(obj);
						}
						that.Model.filterOption.items[index].data = objDeepCopy(arr);
					}
		
				} else {
					that.tips(that, 'warning', data.msg)
				}
		
			});
		},
	
		//点击卡片
		cardEmit(card){
			// let params = objDeepCopy(this.Model.searchFromData);
			// params.tabIndex = card.value;
			//点击卡片 清空下面的搜索条件，并将状态的下拉框对应
			var params = {
				tabIndex:card.value
			}
			this.getMainList(this,params);
			//退款中的状态 （写死）
			if(card.attributeName == 'orderStatus'){
				if(card.attributeValue == null){
					this.Model.filterOption.items[1].value = '';
				}else{
					this.Model.filterOption.items[1].value = String(card.attributeValue);
				}
			}else if(card.attributeName == 'aftersaleStatus'){
				this.Model.filterOption.items[10].value = String(card.attributeValue);
			}
			
		},
		//获取列表
		getMainList(that, params, page) {
			var apiTest = this.apiTest;
			params = params || {
				tabIndex: 20
			}
			page = page || that.Model.page;
			this.$ajax({
				method: 'post',
				url: apiTest + '/sale/saleOrder/list',
				data: {
					condition: params,
					pageIndex: page.pageIndex,
					pageSize: page.pageSize,
				}
			}).then((res) => {
				console.log(res);
				that.Model.tableData = [];
				var data = res.data;
				if (data.status == 200) {
					var obj = {};
					var result = data.result.result;
					var pagination = commonObject.paginationFunction(data.result);
					that.Model.paginationObject = objDeepCopy(pagination);
					var showPay;
					for (var i = 0; i < result.length; i++) {
						obj = objDeepCopy(result[i]);
						switch (result[i].payStatus) {
							case 0:
								showPay = '待支付'
								break;
							case 1:
								showPay = '部分支付'
								break;
							case 2:
								showPay = '已支付'
								break;
							default:
								break;
						}
						obj.showPay = showPay;
						obj.payRatio = obj.payRatio + '%';
						//发货方式
						var showMethod;
						switch (result[i].shippingMethod) {
							case 0:
								showMethod = '快递'
								break;
							case 1:
								showMethod = '自提'
								break;
							default:
								break;
						}
						obj.showMethod = showMethod;

						//订单状态
						var showOrderStatus;
						switch (result[i].orderStatus) {
							case 0:
								showOrderStatus = '待付款'
								break;
							case 1:
								showOrderStatus = '待审核'
								break;
							case 2:
								showOrderStatus = '待发货'
								break;
							case 3:
								showOrderStatus = '待提货'
								break;
							case 4:
								showOrderStatus = '部分已发货'
								break;
							case 5:
								showOrderStatus = '待收货'
								break;
							case 6:
								showOrderStatus = '已完成'
								break;
							case 7:
								showOrderStatus = '已关闭'
								break;
							default:
								break;
						}
						obj.showOrderStatus = showOrderStatus;
						//
						that.Model.tableData.push(obj);
					}

				} else {

				}



			});
		},
		//订单核销
		onOrderConfirm(that, id) {
			var apiTest = this.apiTest;
			this.$ajax({
				method: 'get',
				url: apiTest + '/sale/saleOrder/confirm/' + id,
			}).then((res) => {
				console.log(res);
				var data = res.data;
				if (data.status == 200) {
					that.getMainList(that);
					that.tips(that, 'success', '操作成功');
				} else {
					that.tips(that, 'warning', data.msg);
				}



			});
		},
		//取消订单
		onOrderCancel(that, id) {
			var apiTest = this.apiTest;
			this.$ajax({
				method: 'get',
				url: apiTest + '/sale/saleOrder/cancel/' + id,
			}).then((res) => {
				console.log(res);
				var data = res.data;
				if (data.status == 200) {
					that.getMainList(that)
					that.tips(that, 'success', '操作成功');
				} else {
					that.tips(that, 'warning', data.msg);
				}


			});
		},
		//发货
		onOrderSend(that, id) {
			var apiTest = this.apiTest;
			this.$ajax({
				method: 'get',
				url: apiTest + '/sale/saleOrder/send/' + id,
			}).then((res) => {
				console.log(res);
				var data = res.data;
				if (data.status == 200) {
					that.getMainList(that)
					that.tips(that, 'success', '操作成功');
				} else {
					that.tips(that, 'warning', data.msg);
				}

			});
		},
		//
		//点击 支付
		orderPay(row) {
			this.Model.paySaleOrderId = row.id;
			this.Model.dialogFormVisible = true;
		},
		//确认支付
		comfirmPay() {
			var that = this;
			var apiTest = this.apiTest;
			var params = this.Model.dialogForm;
			params.createTime = getDateString(params.createTime);
			params.saleOrderId = this.Model.paySaleOrderId;
			this.$ajax({
				method: 'post',
				url: apiTest + '/sale/saleOrderPay',
				data: params
			}).then((res) => {
				console.log(res);
				var data = res.data;
				if (data.status == 200) {
					that.getMainList(that);
					that.tips(that, 'success', '操作成功');
					setTimeout(function(){
						that.Model.dialogFormVisible = false;
					},1000)
				} else {
					that.tips(that, 'warning', data.msg);
				}

			});
		},
		//点击 核销，取消订单，发货
		changeOrderStatus(row, type) {
			var that = this;
			this.$confirm('是否确认' + type + '?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
			}).then(() => {
				if (type == '核销') {
					that.onOrderConfirm(that, row.id)
				} else if (type == '取消订单') {
					that.onOrderCancel(that, row.id)
				} else {
					that.onOrderSend(that, row.id)
				}
			}).catch(() => {

			});


		},
		toDetial(row) {
			this.$router.push({
				path: 'spotOrderDetail',
				query: {
					saleNumber: row.saleNumber,
					id: row.id
				}
			});
		},
		newPage() {
			this.$router.push({
				path: 'spotOrderNew',
				query: {

				}
			});
		},
		//
		selectedHighlight(data) {
			// 如果选中则添加高亮样式
			if (this.Model.TableSelectData.indexOf(data.row) > -1) {
				return {
					"background-color": "#FEF6EE"
				};
				return {}
			}
		},
		handleSelectionChange(val) {
			this.Model.TableSelectData = val;
		},
		handleCurrentChange(val) {
			// var searchFromData = this.Model.searchFromData;
			var searchFromData = {
				tabIndex: 20,
			}
			this.Model.page.pageIndex = val;
			this.getMainList(this, searchFromData, this.Model.page);
		},
		handleSizeChange(val) {
			// var searchFromData = this.Model.searchFromData;
			var searchFromData = {
				tabIndex: 20,
			}
			this.Model.page.pageSize = val;
			this.getMainList(this, searchFromData, this.Model.page);
		},
		cardClick(color) {
			this.Model.cardSelected = color;
		},
		showChild(val) {
			let obj;
			obj = {
				keyWord:val.keyWord,
				shipper:val.shipper,
				mobile:val.mobile,
				customerId:val.customerId,
				orderStatus:val.orderStatus,
				reservationNo:val.reservationNo,
				originChannel:val.originChannel,
				salesman:val.salesman,
				shippingMethod:val.shippingMethod,
				payStatus:val.payStatus,
				aftersaleStatusName:val.aftersaleStatusName
			}
			if(val.creatTime){
				obj.bookOrderStartTime = getDateString(val.creatTime[0]);
				obj.bookOrderEndTime = getDateString(val.creatTime[1]);
            }
            switch (val.orderStatus){
				case '0':obj.tabIndex = 21;
					break;
				case '1':obj.tabIndex = 22;
					break;
				case '2':obj.tabIndex = 23;
					break;
				case '3':obj.tabIndex = 24;
					break;
				case '4':obj.tabIndex = 20;
					break;
				case '5':obj.tabIndex = 25;
					break;
				case '6':obj.tabIndex = 20;
					break;
				case '7':obj.tabIndex = 20;
					break;
				default:obj.tabIndex = 20;
					break;
			}
			this.Model.searchFromData = objDeepCopy(obj);
		},
		toSearch(val) {
			var params = this.Model.searchFromData;
			var page = {
				pageIndex: 1,
				pageSize: 10,
			}
			this.getMainList(this, params,page);
		},
		toClear(val) {
			this.Model.searchFromData = this.Model.defaultSearchFromData;
		},
		toExport(val) {
			console.log(val);
		},
		tips(that, type, message) {
			let duration;
			type == 'success' ? duration = 3000 : duration = 10000;
			that.$message({
				message: message,
				type: type,
				duration: duration
			});
		},
		onTabChange(val) {
			console.log(val, this.Model.activeTab)
			switch (val) {
				case 1:
					break;
				case 2:
					break;
				case 3:
					break;
			}
		}
	},
	watch: {
		$route: {
			handler(newValue, oldValue) {
				if (newValue.query.isRefresh) {
					this.init();
				}

			},
			deep: true
		},

	},

}
export default Controller;
